/*
 * ConnectionPanel.fx
 *
 * Created on Apr 26, 2009, 4:13:03 PM
 */

package com.dg.gaming.client.fx.gui;
import com.dg.gaming.api.common.msg.Msg;
import com.dg.gaming.api.common.msg.MsgError;
import com.dg.gaming.api.common.msg.MsgTableConnect;
import com.dg.gaming.api.common.TransmissionException;
import com.dg.gaming.api.common.util.DGLabels;
import com.dg.gaming.client.fx.gui.ConnectionUtil;
import com.dg.gaming.client.IClient;
import com.dg.gaming.client.ITable;
import com.dg.gaming.client.TableConnection;
import java.io.IOException;
import java.lang.Exception;
import java.lang.SecurityException;
import java.lang.String;
import java.lang.System;
import java.net.ConnectException;
import javafx.ext.swing.SwingButton;
import javafx.ext.swing.SwingLabel;
import javafx.ext.swing.SwingTextField;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.Node;
import nanoxml.XMLElement;


/**
 * @author dito
 */

public class ConnectionPanel extends CustomNode, IClient, ITable {
    
    
    public var conn:TableConnection;
    public var mainframe:FXMain;
    var errTxt:String = "";
    var cnp : ConnectionUtil;
    var userName = SwingTextField {
        columns: 10
        text: ""
        editable: true
    }

    var tblId = SwingTextField {
        columns: 10
        text: ""
        editable: true
    }

     var port = SwingTextField {
        columns: 10
        text: "1790"
        editable: true
    }

    var host = SwingTextField {
        columns: 10
        text: "localhost"
        editable: true
    }

    var useNameLb = SwingLabel {
        text: "User Name:"
    }

    var tblLabel = SwingLabel {
        text: "Table Id:"
    }

    var portLabel = SwingLabel {
        text: "Port:"
    }

    var hostLabel = SwingLabel {
        text: "Host:"
    }

    var loginBtn = SwingButton {
        text: "Login"
        action: function() {

            try {
                
                cnp = new ConnectionUtil (this, host.text, Integer.parseInt(port.text), Long.parseLong(userName.text), tblId.text);
                cnp.connect();
                conn=cnp.getConn();
            } catch (coEx: ConnectException ) {
                errTxt = "ConnectException";
            } catch (ioEx: IOException) {
                errTxt = "IOException";
            } catch (e3:SecurityException) {
                errTxt = "IOException";
            } catch (genEx:Exception) {
                errTxt = "Exception";
            }

        }
    }

    var cancelBtn = SwingButton {
        text: "Cancel"
        action: function() {
            System.exit(1);
            
        }
    }


    var group = Group {
       content: [
            VBox {
                content: [
                    HBox {content: [useNameLb, userName]},
                    HBox {content: [tblLabel, tblId]},
                    HBox {content: [portLabel, port]},
                    HBox {content: [hostLabel, host]},
                    HBox {content: [loginBtn, cancelBtn]},
                    HBox {content: [
                                        SwingLabel {
                        text: bind errTxt
                    }

                    ]}
                ]
            }

       ]
    }

    public override  function create():Node {
        return group;
    }


     
    public override function receiveGameMessage(message:XMLElement) {
        var messageType = message.getName();

        // Message could be
        //     <error> - Error message.
        //     <game>  - Game message (success)
        if (messageType.equals(Msg.ERROR)) {
            // Create comm object and create error description
            var commError = new MsgError(message);
            var errorStr = DGLabels.getError(commError.getStatus());

            // Update label
            errTxt = errorStr;
        } else if (messageType.equals(Msg.TABLE)) {
            try {
                var tblconnect = new MsgTableConnect(message);
                var table = tblconnect.getTable();
                // Create game object on connection
                conn.setTable(table);
                conn.setUserName(tblconnect.getUserName());

                for ( elem in  message.getChildren()) {
                    var childMessage =  elem as XMLElement;
                    if (childMessage.getName().equals(Msg.MODEL)) {
                        var model = mainframe.getGameModel();
                        if (model != null) {
                            model.setState(childMessage);
                            table.setModel(model);
                        }
                    }
                }
                // User is sucessfully connected so set game object and return object.
                mainframe.connectOK(conn);
            } catch (tEx: TransmissionException ) {}
        }
    }

    public  override  function receiveTableMessage(message:XMLElement, tableNum:String) {

    }

}
